package com.xuecheng.service.Impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.constant.CourseBaseError;
import com.xuecheng.constant.CoursePublishPreError;
import com.xuecheng.constant.MysqlConstant;
import com.xuecheng.constant.TeachplanError;
import com.xuecheng.exception.CommonError;
import com.xuecheng.exception.XueChengPlusException;
import com.xuecheng.mapper.CoursePublishPreMapper;
import com.xuecheng.model.po.MqMessage;
import com.xuecheng.po.CourseBase;
import com.xuecheng.po.CourseMarket;
import com.xuecheng.po.CoursePublish;
import com.xuecheng.po.CoursePublishPre;
import com.xuecheng.service.*;
import com.xuecheng.vo.CourseBaseInfoVO;
import com.xuecheng.vo.TeachplanVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 课程发布 服务实现类
 */
@Slf4j
@Service
public class CoursePublishPreServiceImpl extends ServiceImpl<CoursePublishPreMapper, CoursePublishPre> implements CoursePublishPreService {
	
	@Resource
	private CourseBaseInfoService courseBaseService;
	@Resource
	private CourseMarketService courseMarketService;
	@Resource
	private TeachplanService teachplanService;
	@Resource
	private CoursePublishService coursePublishService;
	@Resource
	private MqMessageService mqMessageService;
	
	@Transactional
	@Override
	public void commitAudit(Long companyId, Long courseId) {
		//约束校验
		CourseBase courseBase = courseBaseService.getById(courseId);
		//当前审核状态为已提交不允许再次提交
		if (MysqlConstant.COURSEPUBLISHPRE_AUDIT_STATUS_SUBMIT.equals(courseBase.getAuditStatus())) {
			XueChengPlusException.cast(CourseBaseError.COURSE_STATUS_AUDIT);
		}
		//本机构只允许提交本机构的课程
		if (!courseBase.getCompanyId().equals(companyId)) {
			XueChengPlusException.cast(CourseBaseError.COURSE_NOT_MYSELF);
		}
		//课程图片是否填写
		if (StringUtils.isEmpty(courseBase.getPic())) {
			XueChengPlusException.cast(CourseBaseError.COURSE_IMAGE_NULL);
		}
		//添加课程预发布记录
		CoursePublishPre coursePublishPre = new CoursePublishPre();
		//课程基本信息加部分营销信息
		CourseBaseInfoVO courseBaseInfo = courseBaseService.getCourseBaseInfo(courseId);
		BeanUtils.copyProperties(courseBaseInfo, coursePublishPre);
		//课程营销信息
		CourseMarket courseMarket = courseMarketService.getById(courseId);
		//将课程营销信息json数据放入课程预发布表
		coursePublishPre.setMarket(JSON.toJSONString(courseMarket));
		
		//查询课程计划信息
		List<TeachplanVO> teachplanTree = teachplanService.findTeachplanTree(courseId);
		if (teachplanTree == null || teachplanTree.isEmpty()) {
			XueChengPlusException.cast(TeachplanError.Teachplan_NOT_ADD);
		}
		//转json
		coursePublishPre.setTeachplan(JSON.toJSONString(teachplanTree));
		
		//设置预发布记录状态,已提交
		coursePublishPre.setStatus(MysqlConstant.COURSEPUBLISHPRE_AUDIT_STATUS_SUBMIT);
		//教学机构id
		coursePublishPre.setCompanyId(companyId);
		//提交时间
		coursePublishPre.setCreateDate(LocalDateTime.now());
		CoursePublishPre coursePublishPreUpdate = getById(courseId);
		if (coursePublishPreUpdate == null) {
			//添加课程预发布记录
			save(coursePublishPre);
		} else {
			updateById(coursePublishPre);
		}
		//更新课程基本表的审核状态
		courseBase.setAuditStatus(MysqlConstant.COURSEPUBLISHPRE_AUDIT_STATUS_SUBMIT);
		courseBaseService.updateById(courseBase);
	}
	
	@Transactional
	@Override
	public void publish(Long companyId, Long courseId) {
		//查询课程预发布表
		CoursePublishPre coursePublishPre = getById(courseId);
		if (coursePublishPre == null) {
			XueChengPlusException.cast(CoursePublishPreError.COURSEPUBLISHPRE_UNSUBMIT);
		}
		//本机构只允许提交本机构的课程
		if (!coursePublishPre.getCompanyId().equals(companyId)) {
			XueChengPlusException.cast(CoursePublishPreError.COURSEPUBLISHPRE_NOT_MYSELF);
		}
		//审核通过方可发布
		if (!MysqlConstant.COURSEPUBLISHPR_AUDIT_STATUS_APPROVED.equals(coursePublishPre.getStatus())) {
			XueChengPlusException.cast(CoursePublishPreError.COURSEPUBLISHPRE_APPROAL_REQUIRED);
		}
		//保存课程发布信息
		saveCoursePublish(courseId);
		//保存消息表
		saveCoursePublishMessage(courseId);
		//删除课程预发布表对应记录
		removeById(courseId);
	}
	
	/**
	 * 整合课程发布信息
	 */
	private void saveCoursePublish(Long courseId) {
		//查询课程预发布表
		CoursePublishPre coursePublishPre = getById(courseId);
		if (coursePublishPre == null) {
			XueChengPlusException.cast(CoursePublishPreError.COURSEPUBLISHPRE_EMPTY);
		}
		CoursePublish coursePublish = new CoursePublish();
		//拷贝到课程发布对象
		BeanUtils.copyProperties(coursePublishPre, coursePublish);
		coursePublish.setStatus(MysqlConstant.COURSEPUBLISHPR_RELEASE_STATUS);
		coursePublish.setCreateDate(LocalDateTime.now());
		CoursePublish coursePublishUpdate = coursePublishService.getById(courseId);
		if (coursePublishUpdate == null) {
			coursePublishService.save(coursePublish);
		} else {
			coursePublishService.updateById(coursePublish);
		}
		//更新课程基本表的发布状态
		CourseBase courseBase = courseBaseService.getById(courseId);
		courseBase.setStatus(MysqlConstant.COURSEPUBLISHPR_RELEASE_STATUS);
		courseBaseService.updateById(courseBase);
	}
	
	/**
	 * 保存消息表记录
	 */
	private void saveCoursePublishMessage(Long courseId) {
		MqMessage mqMessage = mqMessageService.addMessage("course_publish", String.valueOf(courseId), null, null);
		if (mqMessage == null) {
			log.error("保存消息记录：{}", CommonError.UNKOWN_ERROR);
			XueChengPlusException.cast(CommonError.UNKOWN_ERROR);
		}
	}
}
